package world;

import game.Game;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Input;
import org.newdawn.slick.state.StateBasedGame;

public class InputHandler {

	
	public void world(Input input, int delta, StateBasedGame sbg){

		//pan up	
		if(input.isKeyDown(Input.KEY_UP) && input.isKeyDown(Input.KEY_LSHIFT) || input.isKeyDown(Input.KEY_W) && input.isKeyDown(Input.KEY_LSHIFT)){
		World.viewY += delta * .5f;
			
			if(World.viewY >=0){World.viewY -= delta * .5f;}
			
		}else if(input.isKeyDown(Input.KEY_UP)  || input.isKeyDown(Input.KEY_W)){
			World.viewY += delta * .2f;
			
			if(World.viewY >=0){World.viewY -= delta * .2f;}
		}
		//pan down
		if(input.isKeyDown(Input.KEY_DOWN)  && input.isKeyDown(Input.KEY_LSHIFT) || input.isKeyDown(Input.KEY_S) && input.isKeyDown(Input.KEY_LSHIFT)){
			
			World.viewY -= delta * .5f;
			
			if(Math.abs(World.viewY) >= (World.worldMap.getHeight()) - Game.HEIGHT){World.viewY += delta * .5f;}
			
		}else 
			if(input.isKeyDown(Input.KEY_DOWN)  || input.isKeyDown(Input.KEY_S)){
			
				World.viewY -= delta * .2f;
			
			if(Math.abs(World.viewY) >= (World.worldMap.getHeight()) - Game.HEIGHT){World.viewY += delta * .2f;}
		}

		//pan left
		if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_LSHIFT) || input.isKeyDown(Input.KEY_A) && input.isKeyDown(Input.KEY_LSHIFT) ){
			
			World.viewX += delta * .5f;
			if(World.viewX >= 0){World.viewX -= delta * .5f;}
			
		}else if(input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_A)){
			World.viewX += delta * .2f;
				if(World.viewX >= 0){World.viewX -= delta * .2f;}
		}
		//pan right
		if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_LSHIFT) || input.isKeyDown(Input.KEY_D) && input.isKeyDown(Input.KEY_LSHIFT)){
			
			World.viewX -= delta * .5f;
			if(Math.abs(World.viewX) >= (World.worldMap.getWidth()) - Game.WIDTH){World.viewX += delta * .5f;}
			
		}else if(input.isKeyDown(Input.KEY_RIGHT)  || input.isKeyDown(Input.KEY_D)){
			World.viewX -= delta * .2f;
				if(Math.abs(World.viewX) >= (World.worldMap.getWidth()) - Game.WIDTH){World.viewX += delta * .2f;}

		}
		
		if(input.isKeyPressed(Input.KEY_Q)){if(World.stores.getPaths().get(2).isActive()){System.out.println(World.stores.deactivatePath(World.stores.getPaths().get(2)));}else{System.out.println(World.stores.activatePath(World.stores.getPaths().get(2)));}}
		if(input.isKeyPressed(Input.KEY_F1)){World.debug = !World.debug;}
		if(input.isKeyPressed(Input.KEY_ESCAPE)){World.quit = true;}
		if(input.isKeyDown(Input.KEY_R)  && World.quit){World.quit = false;}
		if(input.isKeyDown(Input.KEY_M) && World.quit){sbg.enterState(0);}
		if(input.isKeyDown(Input.KEY_Q)  && World.quit){System.exit(0);}

		if(Mouse.isButtonDown(0)){

		for(int i = 0; i< World.stores.getEvents().size();i++){
			
		Event check = World.stores.getEvents().get(i);

		if(check.isVisible()){
			int x = (int)check.getX();
				int y = (int)check.getY();	
					if(Math.abs(World.viewX - Mouse.getX()) >= (x - 25) && Math.abs(World.viewX - Mouse.getX()) <= (x + 25)){	
						if(Math.abs(World.viewY + (Mouse.getY() - Game.HEIGHT)) >= (y - 25) && Math.abs(World.viewY + (Mouse.getY() - Game.HEIGHT)) <= (y + 25)){			
					
							if(!Pathing.run && check != Player.event){
								System.out.println("clicked " +  check.getName() + " @" +i);
							Pathing.buildRoute(check);
							}
							
							return;		
						}			
					}
				}
			}
		}

		if(input.isKeyPressed(Input.KEY_E)){

		}

		if(input.isKeyPressed(Input.KEY_SPACE)){

			World.centerView();

		}
			
			}

	
}
